﻿using System;
using System.Windows.Forms;

using Daq331m;

namespace DAC_Demo
{
    public partial class Form1 : Form
    {
        private readonly Daq331M _daq331;
        public Form1()
        {
            InitializeComponent();

            _daq331 = new Daq331M();
        }

        private void ConnectPushButton_Click(object sender, EventArgs e)
        {
            _daq331.DeleteDevice();
            _daq331.CreateDevice();

            _daq331.UseTcpClientBackend("192.168.1.100", 8000);

            // 初始化设备
            Console.WriteLine(!_daq331.InitializeDevice() ? "DAQ331M设备初始化失败" : "DAQ331M设备初始化成功");
        }

        private void ACPushButton_Click(object sender, EventArgs e)
        {
            var dacChannel = (uint)DaqDacChannel.Dac1;
            _daq331.StopDAC(dacChannel);
            var dacMode = (uint)DaqDacMode.Wave;
            _daq331.ConfigDACMode(dacChannel, dacMode);
            /* 输出波形为 vpp：5v frequent:100hz 的正弦波 */
            var frequent = 100;
            var vpp = 5.0;
            var waveData = new double[1000];
            var tempPi = 2 * 3.14159 / 1000;
            for (var i = 1; i <= 1000; i++)
            {
                waveData[i - 1] = 0.5 * vpp * (Math.Sin(tempPi * i) + 1) * 0.273; // 这里的0.273是为了将数据转换为0-15V的范围
            }
            _daq331.ConfigDACWaveData(dacChannel, (uint)frequent, waveData);
            Console.WriteLine(!_daq331.StartDAC(dacChannel) ? "DAQ331M输出失败" : "DAQ331M通道1输出正弦波");
        }

        private void DCPushButton_Click(object sender, EventArgs e)
        {
            var dacChannel = (uint)DaqDacChannel.Dac1;
            _daq331.StopDAC(dacChannel);
            var dacMode = (uint)DaqDacMode.DirectCurrent;
            _daq331.ConfigDACMode(dacChannel, dacMode);
            /* 输出直流 v：10v 范围为0.000V - 15.000V */
            var dacDcData = 10.0;
            _daq331.ConfigDACDirectCurrentData(dacChannel, dacDcData);
            Console.WriteLine(!_daq331.StartDAC(dacChannel) ? "DAQ331M输出失败" : "DAQ331M通道1输出直流");
        }
    }
}
